export const post = (url: string, params: object = {}) => {
	return new Promise((resove, reject) => {
		const token: string = uni.getStorageSync('token') || ''
		uni.request({
			url,
			data: params,
			header: {
				'ba-user-token': token,
				'server':1,
				'lat': uni.getStorageSync('lat')||'',
				'lng': uni.getStorageSync('lng')||'',
				'shop-code':uni.getStorageSync('shopinfo').shop_code||''
			},
			method: 'POST',
			timeout: 20000,
			success(res: any) {
				console.log(res,999)
				switch (res.data.code) {
					case 1001:
					
					break
					case 500:
					uni.showToast({
						icon: "none",
						title:res.data.msg
					})
					break
					case 409:
					if(url.indexOf('api/center/index')>-1){
						return
					}
					uni.showToast({
						icon: "none",
						title: "登录已失效，请重新登录"
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/my/my'
						})
					}, 1500)
					break
					case 303:
					if(url.indexOf('api/center/index')>-1){
						return
					}
					if (uni.getStorageSync('token')) {
						uni.removeStorageSync("token")
						uni.showToast({
							icon: "none",
							title: "登录已失效，请重新登录"
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/my/my'
							})
						}, 1500)
					} else {
						uni.showToast({
							icon: "none",
							title: "请登录后使用"
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/my/my'
							})
						}, 1500)
					}
					resove(res.data)
					break
					case 600:
					uni.showToast({
						icon: "none",
						title:res.data.msg
					})
					break
					
					default:
						resove(res.data)
				}
			},
			fail(err: any) {
				console.log(err,2112)
				switch (err.data.code) {
					default:
						resove(err.data)
				}
				reject(err)
			},
			complete(res: any) {
				if (res.errMsg == "request:fail timeout") {
					uni.showToast({
						title: "请求已超时!",
						icon: "none"
					})
				}
			}
		})
	})
}

export const postLoading = (url: string, params: object = {}) => {
	return new Promise((resove, reject) => {
		const token: string = uni.getStorageSync('token') || ''
		uni.showLoading({
			mask: true,
			title: '加载中',
		})
		uni.request({
			url,
			data: params,
			header: {
				'ba-user-token': token,
				'server':1,
				'lat': uni.getStorageSync('lat')||'',
				'lng': uni.getStorageSync('lng')||'',
				'shop-code':uni.getStorageSync('shopinfo').shop_code||''
			},
			method: 'POST',
			timeout: 20000,
			success(res: any) {
				uni.hideLoading()
				switch (res.data.code) {
					case 1001:
					
					break
					case 500:
					uni.showToast({
						icon: "none",
						title:res.data.msg
					})
					break
					case 0:
					uni.showToast({
						icon: "none",
						title:res.data.msg
					})
					break
					case 302:
					if(url.indexOf('api/center/index')>-1){
						return
					}
						if (uni.getStorageSync('token')) {
							uni.removeStorageSync("token")
							uni.showToast({
								icon: "none",
								title: "登录已失效，请重新登录"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						} else {
							uni.showToast({
								icon: "none",
								title: "请登录后使用"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						}
						break
						case 409:
						if(url.indexOf('api/center/index')>-1){
							return
						}
							if (uni.getStorageSync('token')) {
								uni.removeStorageSync("token")
								uni.showToast({
									icon: "none",
									title: "登录已失效，请重新登录"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							} else {
								uni.showToast({
									icon: "none",
									title: "请登录后使用"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							}
							break
					default:
						resove(res.data)
				}
			},
			fail(err: any) {
				switch (err.data.code) {
					case 302:
					if(url.indexOf('api/center/index')>-1){
						return
					}
						if (uni.getStorageSync('token')) {
							uni.removeStorageSync("token")
							uni.showToast({
								icon: "none",
								title: "登录已失效，请重新登录"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						} else {
							uni.showToast({
								icon: "none",
								title: "请登录后使用"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						}
						break
						case 409:
						if(url.indexOf('api/center/index')>-1){
							return
						}
							if (uni.getStorageSync('token')) {
								uni.removeStorageSync("token")
								uni.showToast({
									icon: "none",
									title: "登录已失效，请重新登录"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							} else {
								uni.showToast({
									icon: "none",
									title: "请登录后使用"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							}
							break
					default:
						resove(err.data)
				}
				uni.hideLoading()
				reject(err)
			},
			complete(res: any) {
				if (res.errMsg == "request:fail timeout") {
					uni.showToast({
						title: "请求已超时!",
						icon: "none"
					})
				}
			}
		})
	})
}

export const get = (url: string, params: object = {}) => {
	return new Promise((resove, reject) => {
		const token: string = uni.getStorageSync('token') || ''
			uni.showLoading({
			mask: true,
			title: '加载中',
			})
		uni.request({
			url,
			data: params,
			header: {
				'ba-user-token': token,
				'server':1,
				'lat': uni.getStorageSync('lat')||'',
				'lng': uni.getStorageSync('lng')||'',
				'shop-code':uni.getStorageSync('shopinfo').shop_code||''
			},
			method: 'GET',
			timeout: 20000,
			success(res: any) {
				uni.hideLoading()
				resove(res.data)
				switch (res.data.code) {
					case 1001:
					break
					case 302:
					if(url.indexOf('api/center/index')>-1){
						return
					}
						if (uni.getStorageSync('token')) {
							uni.removeStorageSync("token")
							uni.showToast({
								icon: "none",
								title: "登录已失效，请重新登录"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						} else {
							uni.showToast({
								icon: "none",
								title: "请登录后使用"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						}
						break
						case 409:
						if(url.indexOf('api/center/index')>-1){
							return
						}
							if (uni.getStorageSync('token')) {
								uni.removeStorageSync("token")
								uni.showToast({
									icon: "none",
									title: "登录已失效，请重新登录"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							} else {
								uni.showToast({
									icon: "none",
									title: "请登录后使用"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							}
							break
						case 500:
						uni.showToast({
							icon: "none",
							title:res.data.msg
						})
						break
						case 0:
						uni.showToast({
							icon: "none",
							title:res.data.msg
						})
						break
					default:
						resove(res.data)
				}
			},
			fail(err: any) {
				reject(err)
			},
			complete(res: any) {
				uni.hideLoading()
				if (res.errMsg == "request:fail timeout") {
					uni.showToast({
						title: "请求已超时!",
						icon: "none"
					})
				}
			}
		})
	})
}

export const getnolog = (url: string, params: object = {}) => {
	return new Promise((resove, reject) => {
		const token: string = uni.getStorageSync('token') || ''
		uni.request({
			url,
			data: params,
			header: {
				'ba-user-token': token,
				'server':1,
				'lat': uni.getStorageSync('lat')||'',
				'lng': uni.getStorageSync('lng')||'',
				'shop-code':uni.getStorageSync('shopinfo').shop_code||''
			},
			method: 'GET',
			timeout: 20000,
			success(res: any) {
				resove(res.data)
				switch (res.data.code) {
					case 1001:
					
					break
					case 302:
					if(url.indexOf('api/center/index')>-1){
						return
					}
						if (uni.getStorageSync('token')) {
							uni.removeStorageSync("token")
							uni.showToast({
								icon: "none",
								title: "登录已失效，请重新登录"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						} else {
							uni.showToast({
								icon: "none",
								title: "请登录后使用"
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/my/my'
								})
							}, 1500)
						}
						break
						case 409:
						if(url.indexOf('api/center/index')>-1){
							return
						}
							if (uni.getStorageSync('token')) {
								uni.removeStorageSync("token")
								uni.showToast({
									icon: "none",
									title: "登录已失效，请重新登录"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							} else {
								uni.showToast({
									icon: "none",
									title: "请登录后使用"
								})
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/my/my'
									})
								}, 1500)
							}
							break
						case 500:
						uni.showToast({
							icon: "none",
							title:res.data.msg
						})
						break
						case 0:
						uni.showToast({
							icon: "none",
							title:res.data.msg
						})
						break
					default:
						resove(res.data)
				}
			},
			fail(err: any) {
				reject(err)
			},
			complete(res: any) {
				uni.hideLoading()
				if (res.errMsg == "request:fail timeout") {
					uni.showToast({
						title: "请求已超时!",
						icon: "none"
					})
				}
			}
		})
	})
}

export const uploadFile = (url: string, filePath: string) => {
	const token: string = uni.getStorageSync('token') || ''
	const lang: string = uni.getStorageSync('lang') || ''
	return new Promise((resove, reject) => {
		uni.uploadFile({
			url,
			filePath,
			name: 'file',
			header: {
				'ba-user-token': token,
				'server':1,
			},
			fileType:'image',
			timeout: 20000,
			success: (res: any) => {
				let data = JSON.parse(res.data)
				resove(data)
			},
			fail: (err: any) => {
				reject(err)
			},
			complete: (res: any) => {
				if (res.errMsg == "request:fail timeout") {
					uni.showToast({
						title: "上传已超时!",
						icon: "none"
					})
				}
			}
		})
	})
}

